/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import domain.model.rms.UsuarioConfig;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import util.J2meAppHelper;

/**
 *
 * @author Victorino
 */
public class BuscaForm implements AppForm, CommandListener {

    private TextBox textBox;
    private Command cmdExe = new Command("Buscar", Command.ITEM, 0);
    private Command cmdVoltar = new Command("Voltar", Command.BACK, 2);

    public BuscaForm() {
        textBox = new TextBox("Digite a palava:", null, 100, TextField.ANY);
        textBox.addCommand(cmdExe);
        textBox.addCommand(cmdVoltar);
        textBox.setCommandListener(this);
    }

    public Displayable getDisplyable() {
        return textBox;
    }

    public void commandAction(Command arg0, Displayable arg1) {

        if (arg0.equals(cmdExe)) {
            String word = textBox.getString();

            if (word.trim().length() == 1) {
                if (!Character.isDigit(word.charAt(0))) {
                    Alert al = new Alert("Aviso", "Seja mais específico em sua busca.", null, AlertType.WARNING);
                    J2meAppHelper.getInstance().changeForm(al, this);
                    return;
                }
            }

            UsuarioConfig uc = J2meAppHelper.getInstance().getUc();
            uc.setUltimaBusca(word);
            uc.save();
           
            J2meAppHelper.getInstance().changeForm(new RegistroForm(), word, true);
        } else if (arg0.equals(cmdVoltar)) {
            J2meAppHelper.getInstance().changeForm(new CategoriaRaizForm(), null);
        }
    }

    public void initialize(Object parameter) {
        UsuarioConfig uc = J2meAppHelper.getInstance().getUc();
        textBox.setString(uc.getUltimaBusca());
    }
}
